package com.crm.services;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.jdao.IRepairDAO;
import com.crm.model.mdao.DictionariesMapper;
import com.crm.model.mdao.ProductMapper;
import com.crm.model.mdao.RepairMapper;
import com.crm.model.mpojo.Repairorders;
import com.crm.model.pojo.Complaint;
import com.crm.model.pojo.Repairorder;
import com.crm.model.pojo.Timeline;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

/**
 * 维修工单
 * @author 梁箐
 *
 */
@Service
public class RepairServices {
	@Autowired
	RepairMapper repairMapper;
	@Autowired
	IRepairDAO iRepairDAO;
	@Autowired
	DictionariesMapper dictionariesMapper;
	@Autowired
	TimeLineServices timeLineServices;
	@Autowired
	ProductMapper productMapper;
	
	/**
	 * 查询所有
	 * @return
	 */
	public List<Map<String, String>> findall() {
		return repairMapper.selectAll();
	}
	
	/**
	 * 查询所有(导出)
	 * @return
	 */
	public List<Map<String, String>> selectalInfo() {
		return repairMapper.selectAllInfo(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId());
	}
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectPage(int pageNum, int pageSize,Integer statueID,String ID) {
		// 使用分页插件
		System.out.println("页码:" + pageNum + ",页数:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer statue = null;
		Integer schedule = null;
		//对状态进行判断
		if (statueID!=null) {
			if (statueID==1264 || statueID==1265 || statueID==1266) {
				
				statue=statueID;
			}
			if (statueID==1431 || statueID==1433 || statueID==1434 || statueID==1436) {
				schedule=statueID;
			}
		}
		System.out.println(statue);
		System.out.println("编号："+statueID+"\t"+schedule);
		// 获取记录数
		List<Map<String, String>> list = repairMapper.selectAllNew(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId(),statue,schedule,ID);
		System.out.println("list :"+list);
	    return list;
	}
	
	/**
	 * 根据客户编号查询维修单
	 * @param custid
	 * @return
	 */
	public List<Map<String, String>> selectcustToRepire(Integer custid) {
		return repairMapper.selectConbycustId(custid);
	}	
	
	//根据工单编号查询
	public Repairorder findByid(Integer roid) {
		return iRepairDAO.findById(roid).get();
	}
	
	//根据工单编号查询（删除）
	public List<Map<String, String>> findByidelete(Integer roid) {
		return repairMapper.selectByiddelete(roid);
	}
	
	/**
	 * 单条查询
	 * @return 
	 */
	public Map<String, String> selectByid(Integer roid) {
		return repairMapper.selectByid(roid);
	}
	/**
	 * 单条查询
	 * @return 
	 */
	public Map<String, String> selectByidOne(Integer roid) {
		return repairMapper.selectByidOne(roid).get(0);
	}
	
	/**
	 * 物理删除（更改状态）
	 * @return 
	 */
	public int deleteByStatue(Integer roid) {
		return repairMapper.deleteByStatue(roid);
	}
	
	
	/**
	 * 添加维修工单
	 */
	public int addRepair(Repairorders repairorders) {
		int insertSelective = repairMapper.insertSelective(repairorders);
		Timeline timeline=new Timeline();
		timeline.setTheme("维修工单");
		timeline.setTimes(new Date());
		timeline.setCustId(repairorders.getCustId());
		Map<String, String> selectByidOne = repairMapper.selectByTheme(repairorders.getRoName());
		System.out.println(repairorders.getRoName());
		System.out.println("时间线："+selectByidOne.get("custName") + selectByidOne.get("goodName"));
		timeline.setDetails("客户："+selectByidOne.get("custName")+" 维修内容为:--维修商品"+selectByidOne.get("goodName"));
		timeLineServices.insertTimeLine(timeline);
		return insertSelective;
	}
	/**
	 * 更新维修工单
	 */
	public int updateRepair(Repairorders repairorders) {
		//Repairorder save = iRepairDAO.save(repairorder);
		return  repairMapper.updateByPrimaryKeySelective(repairorders);
	}
	
}
